using Lzfy_His_Service.Models;
using Lzfy_His_Service.Services;
using Microsoft.AspNetCore.Mvc;

namespace Lzfy_His_Service.Controllers
{
    /// <summary>
    /// 患者退费信息查询控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class PatientRefundQueryController : ControllerBase
    {
        private readonly IPatientRefundQueryService _patientRefundQueryService;
        private readonly ILogger<PatientRefundQueryController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="patientRefundQueryService">患者退费信息查询服务</param>
        /// <param name="logger">日志记录器</param>
        public PatientRefundQueryController(
            IPatientRefundQueryService patientRefundQueryService,
            ILogger<PatientRefundQueryController> logger)
        {
            _patientRefundQueryService = patientRefundQueryService;
            _logger = logger;
        }

        /// <summary>
        /// 通过患者ID查询退费信息
        /// </summary>
        /// <param name="request">患者ID查询请求</param>
        /// <returns>患者退费信息列表</returns>
        [HttpPost("get-refund-info")]
        public async Task<ActionResult<ApiResponse<List<PatientRefundInfo>>>> GetPatientRefundInfo([FromBody] PatientIdQueryRequest request)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(request.PatientId))
                {
                    return BadRequest(new ApiResponse<List<PatientRefundInfo>>
                    {
                        Success = false,
                        Message = "患者ID不能为空"
                    });
                }

                var refundInfoList = await _patientRefundQueryService.GetPatientRefundInfoAsync(request.PatientId);

                return Ok(new ApiResponse<List<PatientRefundInfo>>
                {
                    Success = true,
                    Data = refundInfoList,
                    Message = refundInfoList.Count > 0 ? "查询成功" : "未找到该患者的退费记录"
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "查询患者退费信息失败，患者ID: {PatientId}", request.PatientId);
                return StatusCode(500, new ApiResponse<List<PatientRefundInfo>>
                {
                    Success = false,
                    Message = "查询失败，请稍后重试"
                });
            }
        }
    }


}